<?php

class Module_File
{
        private $CI;
    
        private $name = "";
        private $display_name = "";
        private $category = "";
        
        private $status = MODULE_INACTIVE;

        private $php_dir = "";
        private $css_dir = "";
        private $js_dir = "";
        private $img_dir = "";
        private $progress_install = 0;
	
/*
|===============================================================================
| Constructeur
|===============================================================================
*/
	
	public function __construct($name)
	{
            $this->CI =& get_instance();
            $this->name = $name;
            $this->checkStatus();
	}
        
        private function checkStatus(){
            $this->CI->load->helper('directory');
            
            $module_map = directory_map(MODULE_REPOSITORY . '/' . $this->name);
            foreach ($module_map as $k => $v)
            {
                if (!is_array($v)){
                    if($v == 'module.xml'):
                        $properties = simplexml_load_file(MODULE_REPOSITORY . '/' . $this->name. '/' . $v);
                        $this->display_name = $properties->displayName[0].'';
                        $this->category = $properties->category[0].'';
                        $this->php_dir = $properties->resources->php[0]['dir'].'';
                        
                        $this->js_dir = $properties->resources->js[0]['dir'].'';
                        $this->css_dir = $properties->resources->css[0]['dir'].'';
                        $this->img_dir = $properties->resources->img[0]['dir'].'';
                        $this->CI->load->helper('file');

                        break;
                    elseif($v == 'views.xml') :
                        $properties = simplexml_load_file(MODULE_REPOSITORY . '/' . $this->name. '/' . $v);
                    endif;
                }
            }
            
            //$this->retrieveCSSFiles();
        }
        
        private function transform_fieldXML($fieldXML){
            $attrs = $fieldXML->attributes();
            
            $transformed = isset($attrs['name']) ? $attrs['name'] : '';
            $transformed .= isset($attrs['type']) ? ' ' . $attrs['type'] : '';
            $transformed .= isset($attrs['length']) ? '(' . $attrs['length'] . ')' : '';
            $transformed .= isset($attrs['null']) ? $attrs['null'] == 'false' ? ' NOT NULL' : '' : '';
            $transformed .= isset($attrs['increment']) ? $attrs['increment'] == 'true' ? ' AUTO_INCREMENT' : '' : '';
            $transformed .= isset($attrs['index']) ? ', ' . $attrs['index'] . ' key (' . $attrs['name'] . ')' : '';
            
            return $transformed;
        }
        
        public function getCSSDirectory(){
            return $this->css_dir;
        }        
        
        public function getPHPDirectory(){
            return $this->php_dir;
        }
        
        public function getStatus(){
            return $this->status;
        }
        
        public function getCategory(){
            return $this->category;
        }
        
        public function install(){
            $db = $this->CI->db;
            if(!$this->is_installed()):
                $this->progress_install = 25;
                $module_data = array(
                    'name' => $this->name,
                    'display_name' => $this->display_name,
                    'category' => $this->category,
                    'php_dir' => $this->php_dir,
                    'css_dir' => $this->css_dir,
                    'js_dir' => $this->js_dir,
                    'img_dir' => $this->img_dir
                );
                
                $db->insert('module', $module_data);
                $this->progress_install = 50;
                
                $properties = simplexml_load_file(MODULE_REPOSITORY . '/' . $this->name . '/install.xml');
                $db = $properties->db;
                $db_attr = $db->attributes();
                $db_tables = $db->children();

                foreach ($db_tables as $table):

                    $table_attr = $table->attributes();
                    $table_fields = $table->children();

                    $sql_table = 'CREATE TABLE IF NOT EXISTS ' . $db_attr['namespace'] . '_' . $table_attr['name'] . ' (';
                    foreach ($table_fields as $field) :
                        $sql_table .= $this->transform_fieldXML($field) . ',';
                    endforeach;
                    $sql_table = substr($sql_table, 0, strlen($sql_table) - 1) . ');';
                    $this->CI->db->query($sql_table);
                endforeach;
                
                $this->progress_install = 100;
            endif;
        }
        
        public function uninstall(){
            if($this->is_installed()):
                $properties = simplexml_load_file(MODULE_REPOSITORY . '/' . $this->name . '/install.xml');
                $db = $properties->db;
                $db_attr = $db->attributes();
                $db_tables = $db->children();
                
                foreach ($db_tables as $table):
                    $table_attr = $table->attributes();
                    $sql_table = 'DROP TABLE IF EXISTS ' . $db_attr['namespace'] . '_' . $table_attr['name'] . ';';
                    $this->CI->db->query($sql_table);
                endforeach;
                $this->CI->db->delete("module", array('name' => $this->name));
            endif;
            
        }
        
        
        public function is_installed()
        {
            $db = $this->CI->db;
            
            $db->query("CREATE TABLE IF NOT EXISTS module(
                id int(11) NOT NULL AUTO_INCREMENT,
                
                name VARCHAR(100) NOT NULL UNIQUE,
                display_name VARCHAR(100) NOT NULL,
                category VARCHAR(100),
                
                php_dir VARCHAR(255) NOT NULL,
                css_dir VARCHAR(255),
                js_dir VARCHAR(255),
                img_dir VARCHAR(255),
                
                allow TEXT,
                deny TEXT,
                
                ajax_mode int(1),
                
                PRIMARY KEY (id));");
            
            $db->where('name',$this->name);
            $query = $db->get('module');
            if ($query->num_rows() > 0){
                return true;
            }
            
            return false;
        }
        
        public function getName(){
            return $this->name;
        }
        
        public function getDisplayName(){
            return $this->display_name;
        }
}

?>
